<!DOCTYPE html><html><head>
      <title>2.TestNG&#x5B66;&#x4E60;&#x7B14;&#x8BB0;-&#x6D4B;&#x8BD5;&#x8BBE;&#x8BA1;&#x6A21;&#x5F0F;</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      
      <link rel="stylesheet" href="file:///c:\Users\Administrator\.vscode\extensions\shd101wyy.markdown-preview-enhanced-0.5.3\node_modules\@shd101wyy\mume\dependencies\katex\katex.min.css">
      
      

      
      
      
      
      
      
      

      <style>
      /**
 * prism.js Github theme based on GitHub's theme.
 * @author Sam Clarke
 */
code[class*="language-"],
pre[class*="language-"] {
  color: #333;
  background: none;
  font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
  text-align: left;
  white-space: pre;
  word-spacing: normal;
  word-break: normal;
  word-wrap: normal;
  line-height: 1.4;

  -moz-tab-size: 8;
  -o-tab-size: 8;
  tab-size: 8;

  -webkit-hyphens: none;
  -moz-hyphens: none;
  -ms-hyphens: none;
  hyphens: none;
}

/* Code blocks */
pre[class*="language-"] {
  padding: .8em;
  overflow: auto;
  /* border: 1px solid #ddd; */
  border-radius: 3px;
  /* background: #fff; */
  background: #f5f5f5;
}

/* Inline code */
:not(pre) > code[class*="language-"] {
  padding: .1em;
  border-radius: .3em;
  white-space: normal;
  background: #f5f5f5;
}

.token.comment,
.token.blockquote {
  color: #969896;
}

.token.cdata {
  color: #183691;
}

.token.doctype,
.token.punctuation,
.token.variable,
.token.macro.property {
  color: #333;
}

.token.operator,
.token.important,
.token.keyword,
.token.rule,
.token.builtin {
  color: #a71d5d;
}

.token.string,
.token.url,
.token.regex,
.token.attr-value {
  color: #183691;
}

.token.property,
.token.number,
.token.boolean,
.token.entity,
.token.atrule,
.token.constant,
.token.symbol,
.token.command,
.token.code {
  color: #0086b3;
}

.token.tag,
.token.selector,
.token.prolog {
  color: #63a35c;
}

.token.function,
.token.namespace,
.token.pseudo-element,
.token.class,
.token.class-name,
.token.pseudo-class,
.token.id,
.token.url-reference .token.variable,
.token.attr-name {
  color: #795da3;
}

.token.entity {
  cursor: help;
}

.token.title,
.token.title .token.punctuation {
  font-weight: bold;
  color: #1d3e81;
}

.token.list {
  color: #ed6a43;
}

.token.inserted {
  background-color: #eaffea;
  color: #55a532;
}

.token.deleted {
  background-color: #ffecec;
  color: #bd2c00;
}

.token.bold {
  font-weight: bold;
}

.token.italic {
  font-style: italic;
}


/* JSON */
.language-json .token.property {
  color: #183691;
}

.language-markup .token.tag .token.punctuation {
  color: #333;
}

/* CSS */
code.language-css,
.language-css .token.function {
  color: #0086b3;
}

/* YAML */
.language-yaml .token.atrule {
  color: #63a35c;
}

code.language-yaml {
  color: #183691;
}

/* Ruby */
.language-ruby .token.function {
  color: #333;
}

/* Markdown */
.language-markdown .token.url {
  color: #795da3;
}

/* Makefile */
.language-makefile .token.symbol {
  color: #795da3;
}

.language-makefile .token.variable {
  color: #183691;
}

.language-makefile .token.builtin {
  color: #0086b3;
}

/* Bash */
.language-bash .token.keyword {
  color: #0086b3;
}

/* highlight */
pre[data-line] {
  position: relative;
  padding: 1em 0 1em 3em;
}
pre[data-line] .line-highlight-wrapper {
  position: absolute;
  top: 0;
  left: 0;
  background-color: transparent;
  display: block;
  width: 100%;
}

pre[data-line] .line-highlight {
  position: absolute;
  left: 0;
  right: 0;
  padding: inherit 0;
  margin-top: 1em;
  background: hsla(24, 20%, 50%,.08);
  background: linear-gradient(to right, hsla(24, 20%, 50%,.1) 70%, hsla(24, 20%, 50%,0));
  pointer-events: none;
  line-height: inherit;
  white-space: pre;
}

pre[data-line] .line-highlight:before, 
pre[data-line] .line-highlight[data-end]:after {
  content: attr(data-start);
  position: absolute;
  top: .4em;
  left: .6em;
  min-width: 1em;
  padding: 0 .5em;
  background-color: hsla(24, 20%, 50%,.4);
  color: hsl(24, 20%, 95%);
  font: bold 65%/1.5 sans-serif;
  text-align: center;
  vertical-align: .3em;
  border-radius: 999px;
  text-shadow: none;
  box-shadow: 0 1px white;
}

pre[data-line] .line-highlight[data-end]:after {
  content: attr(data-end);
  top: auto;
  bottom: .4em;
}html body{font-family:"Helvetica Neue",Helvetica,"Segoe UI",Arial,freesans,sans-serif;font-size:16px;line-height:1.6;color:#333;background-color:#fff;overflow:initial;box-sizing:border-box;word-wrap:break-word}html body>:first-child{margin-top:0}html body h1,html body h2,html body h3,html body h4,html body h5,html body h6{line-height:1.2;margin-top:1em;margin-bottom:16px;color:#000}html body h1{font-size:2.25em;font-weight:300;padding-bottom:.3em}html body h2{font-size:1.75em;font-weight:400;padding-bottom:.3em}html body h3{font-size:1.5em;font-weight:500}html body h4{font-size:1.25em;font-weight:600}html body h5{font-size:1.1em;font-weight:600}html body h6{font-size:1em;font-weight:600}html body h1,html body h2,html body h3,html body h4,html body h5{font-weight:600}html body h5{font-size:1em}html body h6{color:#5c5c5c}html body strong{color:#000}html body del{color:#5c5c5c}html body a:not([href]){color:inherit;text-decoration:none}html body a{color:#08c;text-decoration:none}html body a:hover{color:#00a3f5;text-decoration:none}html body img{max-width:100%}html body>p{margin-top:0;margin-bottom:16px;word-wrap:break-word}html body>ul,html body>ol{margin-bottom:16px}html body ul,html body ol{padding-left:2em}html body ul.no-list,html body ol.no-list{padding:0;list-style-type:none}html body ul ul,html body ul ol,html body ol ol,html body ol ul{margin-top:0;margin-bottom:0}html body li{margin-bottom:0}html body li.task-list-item{list-style:none}html body li>p{margin-top:0;margin-bottom:0}html body .task-list-item-checkbox{margin:0 .2em .25em -1.8em;vertical-align:middle}html body .task-list-item-checkbox:hover{cursor:pointer}html body blockquote{margin:16px 0;font-size:inherit;padding:0 15px;color:#5c5c5c;border-left:4px solid #d6d6d6}html body blockquote>:first-child{margin-top:0}html body blockquote>:last-child{margin-bottom:0}html body hr{height:4px;margin:32px 0;background-color:#d6d6d6;border:0 none}html body table{margin:10px 0 15px 0;border-collapse:collapse;border-spacing:0;display:block;width:100%;overflow:auto;word-break:normal;word-break:keep-all}html body table th{font-weight:bold;color:#000}html body table td,html body table th{border:1px solid #d6d6d6;padding:6px 13px}html body dl{padding:0}html body dl dt{padding:0;margin-top:16px;font-size:1em;font-style:italic;font-weight:bold}html body dl dd{padding:0 16px;margin-bottom:16px}html body code{font-family:Menlo,Monaco,Consolas,'Courier New',monospace;font-size:.85em !important;color:#000;background-color:#f0f0f0;border-radius:3px;padding:.2em 0}html body code::before,html body code::after{letter-spacing:-0.2em;content:"\00a0"}html body pre>code{padding:0;margin:0;font-size:.85em !important;word-break:normal;white-space:pre;background:transparent;border:0}html body .highlight{margin-bottom:16px}html body .highlight pre,html body pre{padding:1em;overflow:auto;font-size:.85em !important;line-height:1.45;border:#d6d6d6;border-radius:3px}html body .highlight pre{margin-bottom:0;word-break:normal}html body pre code,html body pre tt{display:inline;max-width:initial;padding:0;margin:0;overflow:initial;line-height:inherit;word-wrap:normal;background-color:transparent;border:0}html body pre code:before,html body pre tt:before,html body pre code:after,html body pre tt:after{content:normal}html body p,html body blockquote,html body ul,html body ol,html body dl,html body pre{margin-top:0;margin-bottom:16px}html body kbd{color:#000;border:1px solid #d6d6d6;border-bottom:2px solid #c7c7c7;padding:2px 4px;background-color:#f0f0f0;border-radius:3px}@media print{html body{background-color:#fff}html body h1,html body h2,html body h3,html body h4,html body h5,html body h6{color:#000;page-break-after:avoid}html body blockquote{color:#5c5c5c}html body pre{page-break-inside:avoid}html body table{display:table}html body img{display:block;max-width:100%;max-height:100%}html body pre,html body code{word-wrap:break-word;white-space:pre}}.markdown-preview{width:100%;height:100%;box-sizing:border-box}.markdown-preview .pagebreak,.markdown-preview .newpage{page-break-before:always}.markdown-preview pre.line-numbers{position:relative;padding-left:3.8em;counter-reset:linenumber}.markdown-preview pre.line-numbers>code{position:relative}.markdown-preview pre.line-numbers .line-numbers-rows{position:absolute;pointer-events:none;top:1em;font-size:100%;left:0;width:3em;letter-spacing:-1px;border-right:1px solid #999;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.markdown-preview pre.line-numbers .line-numbers-rows>span{pointer-events:none;display:block;counter-increment:linenumber}.markdown-preview pre.line-numbers .line-numbers-rows>span:before{content:counter(linenumber);color:#999;display:block;padding-right:.8em;text-align:right}.markdown-preview .mathjax-exps .MathJax_Display{text-align:center !important}.markdown-preview:not([for="preview"]) .code-chunk .btn-group{display:none}.markdown-preview:not([for="preview"]) .code-chunk .status{display:none}.markdown-preview:not([for="preview"]) .code-chunk .output-div{margin-bottom:16px}.scrollbar-style::-webkit-scrollbar{width:8px}.scrollbar-style::-webkit-scrollbar-track{border-radius:10px;background-color:transparent}.scrollbar-style::-webkit-scrollbar-thumb{border-radius:5px;background-color:rgba(150,150,150,0.66);border:4px solid rgba(150,150,150,0.66);background-clip:content-box}html body[for="html-export"]:not([data-presentation-mode]){position:relative;width:100%;height:100%;top:0;left:0;margin:0;padding:0;overflow:auto}html body[for="html-export"]:not([data-presentation-mode]) .markdown-preview{position:relative;top:0}@media screen and (min-width:914px){html body[for="html-export"]:not([data-presentation-mode]) .markdown-preview{padding:2em calc(50% - 457px + 2em)}}@media screen and (max-width:914px){html body[for="html-export"]:not([data-presentation-mode]) .markdown-preview{padding:2em}}@media screen and (max-width:450px){html body[for="html-export"]:not([data-presentation-mode]) .markdown-preview{font-size:14px !important;padding:1em}}@media print{html body[for="html-export"]:not([data-presentation-mode]) #sidebar-toc-btn{display:none}}html body[for="html-export"]:not([data-presentation-mode]) #sidebar-toc-btn{position:fixed;bottom:8px;left:8px;font-size:28px;cursor:pointer;color:inherit;z-index:99;width:32px;text-align:center;opacity:.4}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] #sidebar-toc-btn{opacity:1}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc{position:fixed;top:0;left:0;width:300px;height:100%;padding:32px 0 48px 0;font-size:14px;box-shadow:0 0 4px rgba(150,150,150,0.33);box-sizing:border-box;overflow:auto;background-color:inherit}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar{width:8px}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-track{border-radius:10px;background-color:transparent}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-thumb{border-radius:5px;background-color:rgba(150,150,150,0.66);border:4px solid rgba(150,150,150,0.66);background-clip:content-box}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc a{text-decoration:none}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc ul{padding:0 1.6em;margin-top:.8em}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc li{margin-bottom:.8em}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc ul{list-style-type:none}html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{left:300px;width:calc(100% -  300px);padding:2em calc(50% - 457px -  150px);margin:0;box-sizing:border-box}@media screen and (max-width:1274px){html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{padding:2em}}@media screen and (max-width:450px){html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview{width:100%}}html body[for="html-export"]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .markdown-preview{left:50%;transform:translateX(-50%)}html body[for="html-export"]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .md-sidebar-toc{display:none}
/* Please visit the URL below for more information: */
/*   https://shd101wyy.github.io/markdown-preview-enhanced/#/customize-css */

      </style>
    </head>
    <body for="html-export">
      <div class="mume markdown-preview  ">
      <h1 class="mume-header" id="centertestng%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0-%E6%B5%8B%E8%AF%95%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8Fcenter"><center>TestNG&#x5B66;&#x4E60;&#x7B14;&#x8BB0;-&#x6D4B;&#x8BD5;&#x8BBE;&#x8BA1;&#x6A21;&#x5F0F;</center></h1>

<h2 class="mume-header" id="%E4%B8%80-%E6%B5%8B%E8%AF%95%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F">&#x4E00;&#x3001;&#x6D4B;&#x8BD5;&#x8BBE;&#x8BA1;&#x6A21;&#x5F0F;</h2>

<h3 class="mume-header" id="11-%E9%92%88%E5%AF%B9%E5%A4%B1%E8%B4%A5%E6%B5%8B%E8%AF%95">1.1 &#x9488;&#x5BF9;&#x5931;&#x8D25;&#x6D4B;&#x8BD5;</h3>

<h4 class="mume-header" id="111-%E6%B5%8B%E8%AF%95%E4%BB%A3%E7%A0%81%E6%98%AF%E5%90%A6%E6%BC%82%E4%BA%AE%E5%9C%B0%E5%A4%84%E7%90%86%E4%BA%86%E5%A4%B1%E8%B4%A5">1.1.1 &#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#x662F;&#x5426;&#x6F02;&#x4EAE;&#x5730;&#x5904;&#x7406;&#x4E86;&#x5931;&#x8D25;</h4>

<p>&#x539F;&#x6765;&#x7684;junit&#x9700;&#x8981;&#x4F7F;&#x7528;try/catch/fail &#x7ED3;&#x6784;&#x6765;&#x5904;&#x7406;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#x7684;&#x7A0B;&#x5E8F;&#xFF0C;testng&#x53EA;&#x9700;&#x8981;&#x5728;@Test&#x6CE8;&#x89E3;&#x4E2D;&#x5236;&#x5B9A;&#x5E0C;&#x671B;&#x629B;&#x51FA;&#x7684;&#x5F02;&#x5E38;&#x6216;&#x8005;&#x5F02;&#x5E38;&#x5217;&#x8868;&#x3002;</p>
<p>expectedExceptions&#x5C5E;&#x6027;&#x53EF;&#x4EE5;&#x662F;&#x4E00;&#x4E2A;&#x5F02;&#x5E38;&#x7C7B;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x662F;&#x591A;&#x4E2A;&#x5F02;&#x5E38;&#x7C7B;&#xFF0C;&#x591A;&#x4E2A;&#x5F02;&#x5E38;&#x7C7B;&#x7684;&#x8BDD;&#x76F4;&#x63A5;&#x4EE5;&#x7C7B;&#x540D;&#x5B58;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x5373;&#x53EF;&#xFF0C;&#x6D4B;&#x8BD5;&#x65B9;&#x6CD5;&#x5185;&#x90E8;&#x8C03;&#x7528;&#x4E4B;&#x540E;&#x53EA;&#x8981;&#x629B;&#x51FA;&#x5217;&#x8868;&#x4E2D;&#x6216;&#x8005;&#x6307;&#x5B9A;&#x5F02;&#x5E38;&#x5373;&#x53EF;&#x6D4B;&#x8BD5;&#x901A;&#x8FC7;&#x3002;</p>
<p>&#x5982;&#x4E0B;&#x662F;&#x4E24;&#x6BB5;&#x4EE3;&#x7801;:</p>
<pre data-role="codeBlock" data-info="java" class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestNGDemo</span><span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">methodThrowsOneException</span><span class="token punctuation">(</span><span class="token keyword">int</span> exceptionType<span class="token punctuation">)</span>
                <span class="token keyword">throws</span> <span class="token class-name">FileNotFoundException</span> <span class="token punctuation">{</span>
            LOGGER<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x5F02;&#x5E38;&#x7684;&#x65B9;&#x6CD5;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">FileNotFoundException</span><span class="token punctuation">(</span><span class="token string">&quot;&#x6587;&#x4EF6;&#x4E0D;&#x5B58;&#x5728;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">methodThrowsMoreThanOneException</span><span class="token punctuation">(</span><span class="token keyword">int</span> exceptionType<span class="token punctuation">)</span>
            <span class="token keyword">throws</span> <span class="token class-name">FileNotFoundException</span><span class="token punctuation">,</span> <span class="token class-name">ArithmeticException</span> <span class="token punctuation">{</span>
        LOGGER<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x629B;&#x51FA;&#x591A;&#x4E8E;&#x4E00;&#x4E2A;&#x5F02;&#x5E38;&#x7684;&#x65B9;&#x6CD5;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">switch</span> <span class="token punctuation">(</span>exceptionType<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">case</span> <span class="token number">1</span><span class="token operator">:</span>
                LOGGER<span class="token punctuation">.</span><span class="token function">debug</span><span class="token punctuation">(</span><span class="token string">&quot;&#x6587;&#x4EF6;&#x4E0D;&#x5B58;&#x5728;&#x5F02;&#x5E38;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">FileNotFoundException</span><span class="token punctuation">(</span><span class="token string">&quot;&#x6587;&#x4EF6;&#x4E0D;&#x5B58;&#x5728;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">case</span> <span class="token number">2</span><span class="token operator">:</span>
                LOGGER<span class="token punctuation">.</span><span class="token function">debug</span><span class="token punctuation">(</span><span class="token string">&quot;&#x7B97;&#x672F;&#x5F02;&#x5E38;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">ArithmeticException</span><span class="token punctuation">(</span><span class="token string">&quot;&#x7B97;&#x672F;&#x5F02;&#x5E38;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">default</span><span class="token operator">:</span>
                <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</pre><p>&#x5982;&#x4E0B;&#x662F;&#x5BF9;&#x8FD9;&#x4E24;&#x6BB5;&#x4EE3;&#x7801;&#x7684;&#x6D4B;&#x8BD5;&#x65B9;&#x6CD5;:</p>
<pre data-role="codeBlock" data-info="java" class="language-java"><span class="token annotation punctuation">@Test</span><span class="token punctuation">(</span>expectedExceptions <span class="token operator">=</span> <span class="token class-name">FileNotFoundException</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">withOneException</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">FileNotFoundException</span> <span class="token punctuation">{</span>
    testNGDemo<span class="token punctuation">.</span><span class="token function">methodThrowsOneException</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token annotation punctuation">@Test</span><span class="token punctuation">(</span>expectedExceptions <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token class-name">FileNotFoundException</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span>
        <span class="token class-name">ArithmeticException</span><span class="token punctuation">.</span><span class="token keyword">class</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">withMoreThanOneExceptionOfFNF</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">throws</span> <span class="token class-name">FileNotFoundException</span><span class="token punctuation">,</span> <span class="token class-name">ArithmeticException</span> <span class="token punctuation">{</span>
    testNGDemo<span class="token punctuation">.</span><span class="token function">methodThrowsMoreThanOneException</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

</pre><h4 class="mume-header" id="112-%E4%BD%95%E6%97%B6%E4%B8%8D%E8%A6%81%E7%94%A8expectedexceptions">1.1.2 &#x4F55;&#x65F6;&#x4E0D;&#x8981;&#x7528;expectedExceptions</h4>

<p>&#x6D4B;&#x8BD5;&#x5F02;&#x5E38;&#x4F7F;&#x7528;expectedExceptions&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6709;&#x4E9B;&#x60C5;&#x51B5;&#x6700;&#x597D;&#x4E0D;&#x8981;&#x4F7F;&#x7528;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#xFF0C;&#x6BD4;&#x5982;&#x81EA;&#x5B9A;&#x4E49;&#x5F02;&#x5E38;&#xFF0C;&#x76EE;&#x7684;&#x662F;&#x4E3A;&#x4E86;&#x589E;&#x52A0;&#x4E00;&#x4E9B;&#x7279;&#x5B9A;&#x7684;&#x4FE1;&#x606F;&#x7ED9;&#x7528;&#x6237;&#xFF0C;&#x6216;&#x8005;&#x8BB0;&#x5F55;&#x65E5;&#x5FD7;&#x7ED9;&#x5F00;&#x53D1;&#x4EBA;&#x5458;&#x89C2;&#x5BDF;&#x548C;&#x5206;&#x6790;&#xFF0C;&#x90A3;&#x53EA;&#x786E;&#x4FDD;&#x80FD;&#x629B;&#x51FA;&#x8FD9;&#x4E9B;&#x5F02;&#x5E38;&#x8FD8;&#x4E0D;&#x8DB3;&#x591F;&#xFF0C;&#x9700;&#x8981;&#x68C0;&#x67E5;&#x7279;&#x5B9A;&#x4FE1;&#x606F;&#x624D;&#x53EF;&#x4EE5;&#x3002;</p>
<p>&#x8FD9;&#x65F6;&#x5019;&#x9700;&#x8981;getMessage&#x6765;&#x786E;&#x5B9A;&#x8FD4;&#x56DE;&#x5F02;&#x5E38;&#x7684;&#x63D0;&#x793A;&#x4FE1;&#x606F;&#xFF0C;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x6700;&#x597D;&#x8FD8;&#x662F;&#x7528;&#x539F;&#x59CB;&#x7684;try&#x3001;catch&#x3001;fail&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#xFF0C;&#x5728;catch&#x4E2D;&#x6DFB;&#x52A0;&#x5FC5;&#x8981;&#x7684;&#x6D4B;&#x8BD5;&#x3002;</p>
<p><strong>&#x53E6;&#x5916;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#xFF0C;&#x9700;&#x8981;&#x8003;&#x8651;&#x56FD;&#x9645;&#x5316;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x9700;&#x8981;&#x7684;&#x8BDD;</strong></p>
<h4 class="mume-header" id="113-%E9%87%8D%E6%96%B0%E6%89%A7%E8%A1%8C%E5%A4%B1%E8%B4%A5%E7%9A%84%E7%94%A8%E4%BE%8Btestng-failxml">1.1.3 &#x91CD;&#x65B0;&#x6267;&#x884C;&#x5931;&#x8D25;&#x7684;&#x7528;&#x4F8B;testng-fail.xml</h4>

<p>&#x5F53;&#x4F60;&#x60F3;&#x6267;&#x884C;&#x5305;&#x542B;&#x5931;&#x8D25;&#x7528;&#x4F8B;&#x7684;&#x6D4B;&#x8BD5;&#x5957;&#x4EF6;&#x65F6;&#xFF0C;T&#x4E8C;&#x5341;&#x5929;NG&#x4F1A;&#x5728;&#x8F93;&#x51FA;&#x76EE;&#x5F55;&#x4E2D;&#xFF08;&#x9ED8;&#x8BA4;test-output&#xFF09;&#x4E0B;&#x81EA;&#x52A8;&#x751F;&#x6210;&#x4E00;&#x4E2A;testng-failed.xml&#x7684;&#x6587;&#x4EF6;&#xFF0C;&#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x5305;&#x542B;&#x4E86;&#x539F;&#x6765;testng.xml&#x4E2D;&#x5931;&#x8D25;&#x7684;&#x65B9;&#x6CD5;&#x6784;&#x6210;&#x7684;&#x5B50;&#x96C6;&#x3002;</p>
<p>&#x5982;&#x4E0B;&#x56FE;&#x6240;&#x793A;&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x4F1A;&#x5728;&#x8BE5;&#x6587;&#x4EF6;&#x4E2D;&#x8BB0;&#x5F55;&#x4E00;&#x4E2A;&#x5931;&#x8D25;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x8FD0;&#x884C;&#x8BE5;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#xFF0C;&#x5219;&#x53EF;&#x4EE5;&#x91CD;&#x65B0;&#x8FD0;&#x884C;&#x5931;&#x8D25;&#x7684;&#x7528;&#x4F8B;&#x3002;</p>
<p><img src="./images/testng%E6%A6%82%E8%A7%88/%E6%89%A7%E8%A1%8C%E4%B8%80%E8%BD%AE%E6%B5%8B%E8%AF%95%E4%B9%8B%E5%90%8E%E8%BE%93%E5%87%BA%E7%9A%84%E9%94%99%E8%AF%AF%E7%94%A8%E4%BE%8B%E7%9A%84%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6.png" alt="&#x81EA;&#x52A8;&#x751F;&#x6210;&#x7684;&#x9519;&#x8BEF;&#x914D;&#x7F6E;&#x6587;&#x4EF6;"></p>
<p>&#x6B64;&#x5904;&#x8BF7;&#x6CE8;&#x610F;:&#x6267;&#x884C;&#x5931;&#x8D25;&#x7684;&#x90A3;&#x4E9B;&#x7528;&#x4F8B;&#xFF0C;&#x5982;&#x679C;&#x5B83;&#x4EEC;&#x6709;&#x4F9D;&#x8D56;&#x7684;&#x6D4B;&#x8BD5;&#xFF0C;&#x5373;&#x4F7F;&#x8FD9;&#x4E9B;&#x4F9D;&#x8D56;&#x7684;&#x6D4B;&#x8BD5;&#x6210;&#x529F;&#x4E86;&#xFF0C;&#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x4E2D;&#x4E5F;&#x4F1A;&#x628A;&#x8FD9;&#x4E9B;&#x5B83;&#x4F9D;&#x8D56;&#x7684;&#x4F46;&#x662F;&#x6267;&#x884C;&#x6210;&#x529F;&#x7684;&#x90A3;&#x4E9B;&#x7528;&#x4F8B;&#x4E5F;&#x5305;&#x542B;&#x8FDB;&#x6765;&#x3002;</p>
<p>&#x5982;&#x4E0B;&#x56FE;&#x6240;&#x793A;&#x7684;&#x4EE3;&#x7801;&#x548C;&#x6267;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<p><img src="./images/testng%E6%A6%82%E8%A7%88/%E6%B5%8B%E8%AF%95%E6%96%B9%E6%B3%95%E5%A4%B1%E8%B4%A5-%E4%BD%86%E4%BE%9D%E8%B5%96%E6%96%B9%E6%B3%95%E6%88%90%E5%8A%9F.png" alt="&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x5931;&#x8D25;&#xFF0C;&#x4F46;&#x662F;&#x4F9D;&#x8D56;&#x65B9;&#x6CD5;&#x6210;&#x529F;&#x4E86;"></p>
<p>&#x5B83;&#x4EA7;&#x751F;&#x7684;testng-failed.xml&#x5982;&#x4E0B;:</p>
<pre data-role="codeBlock" data-info="xml" class="language-xml"><span class="token prolog">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
<span class="token doctype">&lt;!DOCTYPE suite SYSTEM &quot;https://testng.org/testng-1.0.dtd&quot;&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>suite</span> <span class="token attr-name">guice-stage</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>DEVELOPMENT<span class="token punctuation">&quot;</span></span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>Failed suite [Default suite]<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>test</span> <span class="token attr-name">thread-count</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>5<span class="token punctuation">&quot;</span></span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>Default test(failed)<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>classes</span><span class="token punctuation">&gt;</span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>class</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>com.autotest.java.demo.testng.TestNGDemoTest<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>methods</span><span class="token punctuation">&gt;</span></span>
          <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>include</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>testWithDependAlwaysSuccess<span class="token punctuation">&quot;</span></span><span class="token punctuation">/&gt;</span></span>
          <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>include</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>withMoreThanOneExceptionOfArith<span class="token punctuation">&quot;</span></span><span class="token punctuation">/&gt;</span></span>
          <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>include</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>setUpClass<span class="token punctuation">&quot;</span></span><span class="token punctuation">/&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>methods</span><span class="token punctuation">&gt;</span></span>
      <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>class</span><span class="token punctuation">&gt;</span></span> <span class="token comment">&lt;!-- com.autotest.java.demo.testng.TestNGDemoTest --&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>classes</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>test</span><span class="token punctuation">&gt;</span></span> <span class="token comment">&lt;!-- Default test(failed) --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>suite</span><span class="token punctuation">&gt;</span></span> <span class="token comment">&lt;!-- Failed suite [Default suite] --&gt;</span>
</pre><p>&#x5982;&#x6B64;&#x770B;&#x6765;&#xFF0C;&#x4E00;&#x4E2A;&#x901A;&#x5E38;&#x7684;&#x8FD0;&#x884C;&#x6D4B;&#x8BD5;&#x7684;&#x6D41;&#x7A0B;&#x5C31;&#x662F;:</p>
<pre data-role="codeBlock" data-info="shell" class="language-shell">java org.testng.TestNG testng.xml

java org.testng.TestNG test-output/testng-failed.xml

</pre><h3 class="mume-header" id="12-%E5%B7%A5%E5%8E%82">1.2 &#x5DE5;&#x5382;</h3>

<h4 class="mume-header" id="121-factory%E6%B3%A8%E8%A7%A3">1.2.1 @Factory&#x6CE8;&#x89E3;</h4>

<p>&#x901A;&#x5E38;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x5F53;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x547D;&#x4EE4;&#x884C;&#x3001;ant&#x3001;&#x6216;&#x8005;testng.xml&#x5C06;&#x6D4B;&#x8BD5;&#x7C7B;&#x4F20;&#x9012;&#x7ED9;TestNG&#x7684;&#x65F6;&#x5019;&#xFF0C;TestNG&#x4F1A;&#x628A;&#x6240;&#x6709;&#x7684;&#x7C7B;&#x90FD;&#x5B9E;&#x4F8B;&#x5316;(&#x901A;&#x8FC7;&#x8C03;&#x7528;&#x6784;&#x9020;&#x65B9;&#x6CD5;)&#xFF0C;&#x7136;&#x540E;&#x6267;&#x884C;&#x6BCF;&#x4E2A;&#x7C7B;&#x7684;&#x6D4B;&#x8BD5;&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x8FD9;&#x5C31;&#x4EA7;&#x751F;&#x4E86;&#x4E00;&#x4E2A;&#x9650;&#x5236;&#xFF0C;&#x5F53;&#x6709;&#x4E9B;&#x6D4B;&#x8BD5;&#x7C7B;&#xFF0C;&#x9644;&#x5E26;&#x51E0;&#x4E2A;&#x6210;&#x5458;&#x53D8;&#x91CF;&#xFF0C;&#x4E14;&#x9700;&#x8981;&#x521D;&#x59CB;&#x5316;&#x5F97;&#x65F6;&#x5019;&#xFF0C;&#x95EE;&#x9898;&#x5C31;&#x6765;&#x4E86;&#x3002;</p>
<p>&#x4F8B;&#x5982;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre data-role="codeBlock" data-info="java" class="language-java"><span class="token comment">//RectangleModel.java</span>
<span class="token keyword">package</span> <span class="token namespace">com<span class="token punctuation">.</span>autotest<span class="token punctuation">.</span>java<span class="token punctuation">.</span>demo<span class="token punctuation">.</span>testng<span class="token punctuation">.</span>model</span><span class="token punctuation">;</span>

<span class="token comment">/**
 * @author : Administrator
 * @since : 2020&#x5E74;4&#x6708;24&#x65E5; &#x4E0B;&#x5348;6:31:11
 * @see :&#x6B64;&#x5904;&#x7528;&#x4E00;&#x4E2A;&#x77E9;&#x5F62;&#x6765;&#x6D4B;&#x8BD5;
 */</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">RectangleModel</span> <span class="token punctuation">{</span>
	<span class="token keyword">private</span> <span class="token keyword">int</span> width<span class="token punctuation">;</span>
	<span class="token keyword">private</span> <span class="token keyword">int</span> length<span class="token punctuation">;</span>

	<span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getWidth</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> width<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setWidth</span><span class="token punctuation">(</span><span class="token keyword">int</span> width<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>width <span class="token operator">=</span> width<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getLength</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> length<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setLength</span><span class="token punctuation">(</span><span class="token keyword">int</span> length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>length <span class="token operator">=</span> length<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token annotation punctuation">@Override</span>
	<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> <span class="token string">&quot;RectangleModel [width=&quot;</span> <span class="token operator">+</span> width <span class="token operator">+</span> <span class="token string">&quot;, length=&quot;</span> <span class="token operator">+</span> length <span class="token operator">+</span> <span class="token string">&quot;]&quot;</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token keyword">public</span> <span class="token class-name">RectangleModel</span><span class="token punctuation">(</span><span class="token keyword">int</span> width<span class="token punctuation">,</span> <span class="token keyword">int</span> length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">super</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>width <span class="token operator">=</span> width<span class="token punctuation">;</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>length <span class="token operator">=</span> length<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token keyword">public</span> <span class="token class-name">RectangleModel</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">super</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

<span class="token punctuation">}</span>

</pre><p>&#x4E0B;&#x9762;&#x662F;&#x4F9D;&#x8D56;&#x4E8E;&#x8FD9;&#x4E2A;&#x77E9;&#x5F62;&#x7C7B;&#x7684;&#x6D4B;&#x8BD5;&#xFF1A;</p>
<pre data-role="codeBlock" data-info="java" class="language-java"><span class="token comment">//TestNGDemoWithFactoryTest.java</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>testng<span class="token punctuation">.</span>annotations</span><span class="token punctuation">.</span><span class="token class-name">Test</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token namespace">com<span class="token punctuation">.</span>autotest<span class="token punctuation">.</span>java<span class="token punctuation">.</span>demo<span class="token punctuation">.</span>testng<span class="token punctuation">.</span>model</span><span class="token punctuation">.</span><span class="token class-name">RectangleModel</span><span class="token punctuation">;</span>

<span class="token comment">/**
 * @author : Administrator
 * @since : 2020&#x5E74;4&#x6708;24&#x65E5; &#x4E0B;&#x5348;6:30:10
 * @see :
 */</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestNGDemoWithFactoryTest</span> <span class="token punctuation">{</span>
	<span class="token keyword">private</span> <span class="token class-name">RectangleModel</span> rectangleModel<span class="token punctuation">;</span>
	<span class="token keyword">private</span> <span class="token keyword">int</span> width<span class="token punctuation">;</span>
	<span class="token keyword">private</span> <span class="token keyword">int</span> length<span class="token punctuation">;</span>

	<span class="token keyword">public</span> <span class="token class-name">TestNGDemoWithFactoryTest</span><span class="token punctuation">(</span><span class="token keyword">int</span> width<span class="token punctuation">,</span> <span class="token keyword">int</span> length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>rectangleModel <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RectangleModel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token annotation punctuation">@Test</span>
	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testWidth</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

	<span class="token punctuation">}</span>

	<span class="token annotation punctuation">@Test</span>
	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testLength</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</pre><p>&#x8FD0;&#x884C;&#x4E0A;&#x8FF0;&#x6D4B;&#x8BD5;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5219;&#x4F1A;&#x62A5;&#x9519;,&#x5927;&#x81F4;&#x610F;&#x601D;&#x662F;&#x65E0;&#x6CD5;&#x521D;&#x59CB;&#x5316;&#x8FD9;&#x4E2A;&#x6D4B;&#x8BD5;&#x7C7B;&#xFF1A;</p>
<pre data-role="codeBlock" data-info class="language-"><code>[RemoteTestNG] detected TestNG version 7.0.1
org.testng.TestNGException: 
An error occurred while instantiating class com.autotest.java.demo.testng.TestNGDemoWithFactoryTest. Check to make sure it can be instantiated
	at org.testng.internal.InstanceCreator.createInstanceUsingObjectFactory(InstanceCreator.java:134)
	at org.testng.internal.InstanceCreator.createInstance(InstanceCreator.java:79)
	at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:109)
	at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:167)
	at org.testng.TestClass.getInstances(TestClass.java:102)
	at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:82)
	at org.testng.TestClass.init(TestClass.java:74)
	at org.testng.TestClass.&lt;init&gt;(TestClass.java:39)
	at org.testng.TestRunner.initMethods(TestRunner.java:455)
	at org.testng.TestRunner.init(TestRunner.java:339)
	at org.testng.TestRunner.init(TestRunner.java:292)
	at org.testng.TestRunner.&lt;init&gt;(TestRunner.java:223)
	at org.testng.remote.support.RemoteTestNG6_12$1.newTestRunner(RemoteTestNG6_12.java:33)
	at org.testng.remote.support.RemoteTestNG6_12$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_12.java:66)
	at org.testng.ITestRunnerFactory.newTestRunner(ITestRunnerFactory.java:55)
	at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:676)
	at org.testng.SuiteRunner.init(SuiteRunner.java:178)
	at org.testng.SuiteRunner.&lt;init&gt;(SuiteRunner.java:112)
	at org.testng.TestNG.createSuiteRunner(TestNG.java:1291)
	at org.testng.TestNG.createSuiteRunners(TestNG.java:1278)
	at org.testng.TestNG.runSuitesLocally(TestNG.java:1127)
	at org.testng.TestNG.runSuites(TestNG.java:1066)
	at org.testng.TestNG.run(TestNG.java:1034)
	at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
	at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

</code></pre><p>&#x89E3;&#x51B3;&#x65B9;&#x6CD5;&#xFF1A;<br>
&#x53EF;&#x4EE5;&#x7528;@Factory&#x6CE8;&#x89E3;,&#x52A0;&#x4E0A;&#x5982;&#x4E0B;&#x4EE3;&#x7801;:</p>
<pre data-role="codeBlock" data-info="java" class="language-java"><span class="token comment">//TestNGDemoWithFactoryTest.java</span>
<span class="token keyword">import</span> <span class="token namespace">org<span class="token punctuation">.</span>testng<span class="token punctuation">.</span>annotations</span><span class="token punctuation">.</span><span class="token class-name">Factory</span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@Factory</span>
	<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">Object</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">create</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">List</span> result <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

		result<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">TestNGDemoWithFactoryTest</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		result<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">TestNGDemoWithFactoryTest</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

		<span class="token keyword">return</span> result<span class="token punctuation">.</span><span class="token function">toArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token punctuation">}</span>

</pre><p>&#x5982;&#x56FE;&#x662F;&#x589E;&#x52A0;&#x5DE5;&#x5382;&#x4E4B;&#x540E;&#x7684;&#x6267;&#x884C;&#x7ED3;&#x679C;,&#x53EF;&#x4EE5;&#x53D1;&#x73B0;&#x6BCF;&#x4E2A;&#x6D4B;&#x8BD5;&#x65B9;&#x6CD5;&#x90FD;&#x88AB;&#x6267;&#x884C;&#x4E86;&#x4E24;&#x6B21;&#xFF0C;&#x56E0;&#x4E3A;&#x5DE5;&#x5382;&#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x4E86;&#x4E24;&#x4E2A;&#x5B9E;&#x4F8B;&#x3002;</p>
<p><img src="./images/testng%E6%A6%82%E8%A7%88/factory%E6%B3%A8%E8%A7%A3%E6%89%A7%E8%A1%8C%E7%BB%93%E6%9E%9C.png" alt="&#x589E;&#x52A0;Factory&#x6CE8;&#x89E3;&#x4E4B;&#x540E;&#x7684;&#x7ED3;&#x679C;"></p>
<p>&#x5173;&#x4E8E;&#x5DE5;&#x5382;&#x7684;&#x6CE8;&#x610F;&#x70B9;:</p>
<ul>
<li>&#x5B83;&#x9700;&#x8981;&#x6CE8;&#x89E3;&#x5230;&#x4E00;&#x4E2A;&#x8FD4;&#x56DE;&#x5BF9;&#x8C61;&#x6570;&#x7EC4;&#x7684;&#x65B9;&#x6CD5;&#x4E0A;(&#x8FD4;&#x56DE;&#x503C;&#x662F;Object[] )</li>
<li>&#x5B83;&#x4F1A;&#x5728;&#x6240;&#x6709;&#x5F97;Test&#x65B9;&#x6CD5;&#x6267;&#x884C;&#x4E4B;&#x524D;&#x8FD0;&#x884C;</li>
<li>&#x5B83;&#x53EA;&#x4F1A;&#x8FD0;&#x884C;&#x4E00;&#x6B21;</li>
<li>&#x5982;&#x679C;&#x6709;&#x591A;&#x4E2A;&#xFF0C;&#x90A3;&#x4E48;&#x4F1A;&#x628A;&#x6240;&#x6709;&#x5F97;@Factory&#x6CE8;&#x89E3;&#x7684;&#x65B9;&#x6CD5;&#x6267;&#x884C;&#x5B8C;&#x4E4B;&#x540E;&#xFF0C;&#x518D;&#x6267;&#x884C;&#x6D4B;&#x8BD5;</li>
<li>&#x8BF7;&#x628A;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x8BBE;&#x7F6E;&#x4E3A;static&#xFF0C;&#x6216;&#x8005;&#x628A;&#x65E0;&#x53C2;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x63D0;&#x4F9B;&#x51FA;&#x6765;</li>
</ul>
<p>&#x5982;&#x4E0B;&#x662F;&#x5982;&#x679C;&#x628A;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;static&#x53BB;&#x6389;&#xFF0C;&#x5219;&#x4F1A;&#x8FD0;&#x884C;&#x5931;&#x8D25;:</p>
<p><img src="./images/testng%E6%A6%82%E8%A7%88/%E4%B8%8D%E5%B8%A6static%E7%9A%84Factory%E6%96%B9%E6%B3%95.png" alt="&#x53BB;&#x6389;static&#x7684;factory&#x65B9;&#x6CD5;"></p>
<h4 class="mume-header" id="122-%E5%A4%9A%E4%B8%AA%E5%AE%9E%E4%BE%8B%E7%9A%84%E6%B5%8B%E8%AF%95%E4%BF%A1%E6%81%AF">1.2.2 &#x591A;&#x4E2A;&#x5B9E;&#x4F8B;&#x7684;&#x6D4B;&#x8BD5;&#x4FE1;&#x606F;</h4>

<p>&#x5728;&#x4E0A;&#x8282;&#x4E2D;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x53CD;&#x6620;&#x7684;&#x4E0D;&#x662F;&#x90A3;&#x4E48;&#x660E;&#x663E;&#xFF0C;&#x5DF2;&#x7ECF;&#x4E0D;&#x77E5;&#x9053;&#x54EA;&#x4E2A;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x662F;&#x7531;&#x54EA;&#x4E2A;&#x6D4B;&#x8BD5;&#x7C7B;&#x7684;&#x5B9E;&#x4F8B;&#x8FD0;&#x884C;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x5931;&#x8D25;&#x4E86;&#x5E76;&#x4E0D;&#x65B9;&#x4FBF;&#x5B9A;&#x4F4D;&#xFF0C;&#x5728;&#x62A5;&#x544A;&#x4E2D;&#x51FA;&#x73B0;&#x7684;&#x4E5F;&#x4E0D;&#x662F;&#x5F88;&#x660E;&#x663E;&#x3002;</p>
<p>&#x5219;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5B9E;&#x73B0;ITest&#x63A5;&#x53E3;(org.testng.ITest)&#x6765;&#x91CD;&#x5199;getTestName()&#x65B9;&#x6CD5;</p>
<p>&#x5982;&#x4E0B;&#x662F;&#x91CD;&#x5199;&#x540E;&#x7684;&#x6D4B;&#x8BD5;&#x7ED3;&#x679C;,&#x81F3;&#x4E8E;&#x50CF;&#x4FEE;&#x6539;&#x6210;&#x4EC0;&#x4E48;&#x6837;&#x5B50;&#x6765;&#x4F53;&#x73B0;&#x7684;&#x66F4;&#x7CBE;&#x7EC6;&#xFF0C;&#x5219;&#x9700;&#x8981;&#x6162;&#x6162;&#x659F;&#x914C;&#x3002;</p>
<p><img src="./images/testng%E6%A6%82%E8%A7%88/%E9%87%8D%E5%86%99Itest%E5%B9%B6%E4%BF%AE%E6%94%B9%E6%B5%8B%E8%AF%95%E7%94%A8%E4%BE%8B%E5%90%8D%E7%A7%B0.png" alt="&#x5B9E;&#x73B0;Itest&#x91CD;&#x5199;&#x65B9;&#x6CD5;&#x540D;&#x79F0;"></p>
<p>&#x5982;&#x4E0B;&#x662F;&#x5728;&#x6D4B;&#x8BD5;&#x62A5;&#x544A;&#x4E2D;&#x7684;&#x4F53;&#x73B0;:</p>
<p><img src="./images/testng%E6%A6%82%E8%A7%88/%E9%87%8D%E5%86%99itest%E4%B9%8B%E5%90%8E%E7%9A%84%E6%B5%8B%E8%AF%95%E6%8A%A5%E5%91%8A%E5%B1%95%E7%A4%BA.png" alt="&#x5B9E;&#x73B0;Itest&#x4E4B;&#x540E;&#x7684;&#x6D4B;&#x8BD5;&#x62A5;&#x544A;&#x53D8;&#x5316;"></p>
<h3 class="mume-header" id="13-%E6%95%B0%E6%8D%AE%E9%A9%B1%E5%8A%A8%E6%B5%8B%E8%AF%95ddt">1.3 &#x6570;&#x636E;&#x9A71;&#x52A8;&#x6D4B;&#x8BD5;(DDT)</h3>

<p>&#x7531;&#x4E8E;DDT&#x6982;&#x5FF5;&#x6BD4;&#x8F83;&#x5927;&#xFF0C;TestNG&#x4E2D;&#x7684;&#x76F8;&#x5173;&#x4FE1;&#x606F;&#x4E5F;&#x4E0D;&#x5C11;&#xFF0C;&#x56E0;&#x6B64;&#x53E6;&#x6362;&#x4E00;&#x7BC7;&#x51C6;&#x4EEC;&#x4ECB;&#x7ECD;&#x6570;&#x636E;&#x9A71;&#x52A8;&#x3002;</p>

      </div>
      
      
    
    
    
    
    
    
    
    
  
    </body></html>